<?php

namespace App\Http\Controllers\NB\common;

use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\common\PublicController;
use App\Http\Controllers\API\common\NBIotController;
//模型
use App\Models\Building;
use App\Models\Room;

class NbbindController extends PublicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        /*搜索条件始*/
        //搜索条件
        $get['area'] = $request -> input('area','');
        if($get['area']){
            $this -> where .= ' -> where("area.name","like","%".$get["area"]."%")';
        }
        $get['building'] = $request -> input('building','');
        if($get['building']){
            $this -> where .= ' -> where("building.name","like","%".$get["building"]."%")';
        }
        $get['room'] = $request -> input('room','');
        if($get['room']){
            $this -> where .= ' -> where("room.name","like","%".$get["room"]."%")';
        }
        $get['dev'] = $request -> input('dev','');
        if($get['dev']){
            $this -> where .= ' -> where("verifyCode","like","%".$get["dev"]."%")';
        }
        /*搜索条件末*/
        /*代理商搜索始*/
        //获取自身及旗下所有代理商
        $agentInfo = $this -> agentInfo($request -> cookie('logon')['agent_id']);
        /*代理商搜索末*/
        $get['agent_id'] = $this -> agent_id;
        //给页面分配数据
        $nbdevInfo = $this -> nbdevInfo($get);
        return view('NB.common.nbbind.index',['data'=>$nbdevInfo['data'],'info'=>$nbdevInfo['info'],'agentInfo'=>$agentInfo,'request'=>$request -> all(),'get'=>$get,'areaids' => $this->area_ids]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        //获取所有的设备
        $nbdevInfo = DB::table('nb_device') -> select('verifyCode') -> get();
        //获取设备类型
        $typeInfo = DB::table('nb_type') -> get();
        //获取代理商下的所有区域
        if($this -> area_ids === '0'){
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        }else{
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> whereIn("area.id",explode(",",$this -> area_ids)) -> get();
        }
        return view('NB.common.nbbind.create',['nbdevInfo' => $nbdevInfo,'areaInfo'=>$areaInfo,'typeInfo'=>$typeInfo]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['agent_id'] = $this -> agent_id;
        //添加到烟感设备表
        $id = DB::table('nb_bind') -> insertGetId($info);
        if($id){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //修改设备服务状态
    public function postStatus(Request $request)
    {
        $id = $request -> input('id');
        $status = $request -> input('status');
        //查询绑定表
        $info = DB::table('nb_bind as nb')
            ->leftJoin('nb_device as nd','nd.verifyCode','=','nb.verifyCode')
            ->leftJoin('nb_model as nm','nm.id','=','nd.modelId')
            ->leftJoin('nb_abutment as na','na.id','=','nm.abut_id')
            ->select('nd.deviceId','na.appId','na.secret')
            ->where('nb.id',$id)
            ->first();
        //下发指令
        $NBIot = new NBIotController($info['appId'],$info['secret']);
        $output = $NBIot -> create_command($info['deviceId'],'Infrared',$status);
        //修改数据库设备状态
        $res = DB::table('nb_bind') -> where('id',$id) -> update(['status'=>$status]);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        return $data;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        //获取设备类型
        $typeInfo = DB::table('nb_type') -> get();
        //获取烟感设备信息
        $info = DB::table('nb_bind as nb')
            -> leftJoin('room','room_id','=','room.id')
            -> leftJoin('building','building_id','=','building.id')
            -> select('nb.*','building_id','area_id')
            -> where('nb.id',$id)
            -> first();
        //获取代理商下的所有区域
        if($this -> area_ids === '0'){
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        }else{
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> whereIn("area.id",explode(",",$this -> area_ids)) -> get();
        }
        //获取该房间所在区域的所有建筑
        $buildingInfo = Building::where('area_id',$info['area_id']) -> get() -> toarray();
        //获取该房间所在建筑的所有房间
        $roomInfo = Room::where('building_id',$info['building_id']) -> get() -> toarray();
        return view('NB.common.nbbind.edit'        //修改smoke表
            ,['data'=>$info,'areaInfo'=>$areaInfo,'buildingInfo'=>$buildingInfo,'roomInfo'=>$roomInfo,'typeInfo'=>$typeInfo]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('_token','id');
        $res = DB::table('nb_bind') -> where('id',$id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询区域和客户的关系表并删除
        $res = DB::table('nb_bind') -> delete($id);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }
}
